﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web.Hosting;

namespace Telerik.Sitefinity.Services.Migration
{
	public partial class MigrationExportService : IMigrationExportService
	{
		public Message GetThemes()
		{
			List<MigrationTheme> output = new List<MigrationTheme>();

			string[] themes = { };
			try
			{
				themes = Directory.GetDirectories(HostingEnvironment.MapPath("~/App_Themes"));
			}
			catch (IOException) { }
			catch (UnauthorizedAccessException) { };

			for (int i = 0; i < themes.Length; i++)
			{
				string themeName = Path.GetFileName(themes[i]);
				string themePath = Path.GetFullPath(themes[i]);
				MigrationTheme mt = new MigrationTheme();
				mt.ThemeName = themeName;

				FileInfo[] cssFiles = { };
				FileInfo[] gifFiles = { }, jpgFiles = { }, jpegFiles = { }, pngFiles = { }, imageFiles = { };

				try
				{
					DirectoryInfo themeDir = new DirectoryInfo(themes[i]);
					cssFiles = themeDir.GetFiles("*.css", SearchOption.AllDirectories);
					gifFiles = themeDir.GetFiles("*.gif", SearchOption.AllDirectories);
					jpgFiles = themeDir.GetFiles("*.jpg", SearchOption.AllDirectories);
					jpegFiles = themeDir.GetFiles("*.jpeg", SearchOption.AllDirectories);
					pngFiles = themeDir.GetFiles("*.png", SearchOption.AllDirectories);
				}
				catch (IOException) { }
				catch (UnauthorizedAccessException) { };

				imageFiles = gifFiles.Union(jpgFiles).Union(jpegFiles).Union(pngFiles).ToArray();

				for (int j = 0; j < cssFiles.Length; j++)
				{
					// Get the path after ~/App_Themes/<ThemeName> and convert '\' to '_'
					string cssFileName = cssFiles[j].DirectoryName + @"\" + cssFiles[j].Name;
					cssFileName = cssFileName.Replace(themePath, String.Empty);
					cssFileName = cssFileName.Replace('\\', '_');
					if (cssFileName.StartsWith("_")) cssFileName = cssFileName.Substring(1);

					MigrationCssFile mcf = new MigrationCssFile();
					mcf.FileName = cssFileName;

					string cssFileContents = String.Empty;
					try
					{
						cssFileContents = File.ReadAllText(cssFiles[j].DirectoryName + @"\" + cssFiles[j].Name);
					}
					catch (IOException) { }
					catch (System.Security.SecurityException) { }
					catch (UnauthorizedAccessException) { };

					mcf.FileContents = cssFileContents;

					mt.CssFiles.Add(mcf);
				}

				for (int j = 0; j < imageFiles.Length; j++)
				{
					string imageFileName = imageFiles[j].DirectoryName + @"\" + imageFiles[j].Name;
					imageFileName = imageFileName.Replace(themePath, String.Empty);
					if (imageFileName.StartsWith("_")) imageFileName = imageFileName.Substring(1);

					MigrationImageFile mif = new MigrationImageFile();
					mif.FileName = imageFileName;

					byte[] imageFileContents = { };
					try
					{
						imageFileContents = File.ReadAllBytes(imageFiles[j].DirectoryName + @"\" + imageFiles[j].Name);
					}
					catch (IOException) { }
					catch (System.Security.SecurityException) { }
					catch (UnauthorizedAccessException) { };

					mif.ImageData = imageFileContents;

					mt.ImageFiles.Add(mif);
				}

				output.Add(mt);
			}

			MessageVersion ver = OperationContext.Current.IncomingMessageVersion;
			return Message.CreateMessage(ver, "GetThemesResponse", output);
		}
	}
}
